#include "SendMessageThread.h"
#include <QDateTime>
#include <QDebug>


SendMessageThread::SendMessageThread(QSerialPort *serial, Ui::MainWindow *ui, QObject *parent)
    : QThread(parent), serial(serial), ui(ui), send_ok(false)
{

    connect(&commandTimer, &QTimer::timeout, this, &SendMessageThread::processNext);
}

void SendMessageThread::run()
{
    QTimer timer;
    connect(&timer, &QTimer::timeout, this, &SendMessageThread::processSendQueue);
    timer.start(500); // 设置一个定时器，定时执行队列中的发送操作
    exec();
}

void SendMessageThread::processSendQueue()
{
    //qDebug() << "Processing send queue at " << QDateTime::currentDateTime().toString();
    if (!send_ok) {
        return;
    }

    if (serial->isOpen()) {
        QByteArray sendData = ui->textEdit_3->toPlainText().toUtf8();

        if (ui->checkBox->isChecked()) {
            sendData.append("\r\n");
        }

        serial->write(sendData);

        // 填充命令队列
        commandQueue.enqueue([this] { handleSend(ui->checkBox_001, ui->lineEdit_001, "W A001 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_002, ui->lineEdit_002, "W A002 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_003, ui->lineEdit_003, "W A003 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_004, ui->lineEdit_004, "W A004 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_005, ui->lineEdit_005, "W A005 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_006, ui->lineEdit_006, "W A006 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_007, ui->lineEdit_007, "W A007 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_008, ui->lineEdit_008, "W A008 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_009, ui->lineEdit_009, "W A009 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_010, ui->lineEdit_010, "W A010 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_011, ui->lineEdit_011, "W A011 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_012, ui->lineEdit_012, "W A012 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_013, ui->lineEdit_013, "W A013 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_014, ui->lineEdit_014, "W A014 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_015, ui->lineEdit_015, "W A015 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_016, ui->lineEdit_016, "W A016 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_017, ui->lineEdit_017, "W A017 U"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_018, ui->lineEdit_018, "W A018 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_019, ui->lineEdit_019, "W A019 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_020, ui->lineEdit_020, "W A020 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_021, ui->lineEdit_021, "W A021 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_022, ui->lineEdit_022, "W A022 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_021, ui->lineEdit_021, "W A021 S"); });
        commandQueue.enqueue([this] { handleSend(ui->checkBox_022, ui->lineEdit_022, "W A022 S"); });
        commandQueue.enqueue([this] {
            ui->textEdit_2->append("Send message Success!\r");
            ui->textEdit_2->ensureCursorVisible(); });


        // 启动命令处理定时器
        if (!commandQueue.isEmpty()) {
            commandTimer.start(500); // 每500毫秒处理一个命令
        }
        send_ok = false;
    } else {
        ui->textEdit_2->append("ERROR: Serial port Close！\r");
        ui->textEdit_2->ensureCursorVisible();
        send_ok = false;
    }
}

void SendMessageThread::processNext()
{
    if (!commandQueue.isEmpty()) {
        commandQueue.dequeue()();
    } else {
        commandTimer.stop(); // 队列为空时停止定时器
    }
}

void SendMessageThread::handleSend(QCheckBox *checkBox, QLineEdit *lineEdit, const QString &prefix)
{
    if (checkBox && checkBox->isChecked()) {
        QByteArray data = lineEdit->text().toUtf8();
        data.prepend(prefix.toUtf8());
        data.append("#\r\n");
        serial->write(data);
        ui->textEdit_2->append(data);
        ui->textEdit_2->ensureCursorVisible();

    }
}

void SendMessageThread::sendMessage()  // 确保定义sendMessage方法
{
    send_ok = true;
}
